package com.example.listview_zuoye;


import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextWatcher;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    List<String> data,dataBack ;
    String searchTxt="" ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String txt = "|外挂傍身的杂草 "+"|大主宰"+"|大威天龙 大罗法杖 "+"|大爷打乒乓球"+
                "|超级大主簿"+"|我活了一万年"+"|捉鬼系统开启";
        String[] txts = txt.split("\\|");
        //用符号隔开
        data = Arrays.asList(txts);  //接受数据
        dataBack=data ; //dataBaK 用的 data的数据 ，方便重复查询

        EditText et = findViewById(R.id.search);
        ListView lv = findViewById(R.id.lv);
        final MyAdapter myAdapter = new MyAdapter();
        lv.setAdapter(myAdapter);

        /*搜索框 多关键字搜索*/
        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                searchTxt = s.toString();
                String[] ss = searchTxt.split(" ");
                List<String> res = new ArrayList<>();
                for (String t : dataBack){ //data 只有一次搜索 要改成databack 才可以重复搜索
                    boolean found = true;  //如果找到就把它设置为true
                    for (String x : ss){
                        if (x.length() == 0 || x.isEmpty()){
                            continue;
                        }
                        if (!t.contains(x)){  //如果关键字中不包含，就设置为false
                            found = false;
                            break;
                        }
                    }
                    if (found){
                        res.add(t);
                    }
                }
                data = res;
                myAdapter.notifyDataSetChanged();

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
    //    BaseAdapter 方法
    class MyAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null){
                convertView = getLayoutInflater().inflate(R.layout.cell,null);
            }

            String txt =data.get(position);
            TextView tv = convertView.findViewById(R.id.title);


            //搜索关键字变色 方法
            SpannableStringBuilder builder =new SpannableStringBuilder(txt) ; //需要修饰的文本
            String [] key =searchTxt.split(" "); //修饰要求: 在搜索框中--关键字
            for (String k :key){
                int idx=txt.indexOf(k) ;
                ForegroundColorSpan span =new ForegroundColorSpan(Color.parseColor("#00BFFF")) ; //染色
                builder.setSpan(span,idx , idx+k.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }

            tv.setText(builder);
            //把TXT 改成 builder  调用builder
//            tv.setText(txt);
            return convertView;

        }

    }

}